
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Xml;

using Swaf;

using Swaf.BizObj;

namespace Swaf.BizObj.xml
{
	/// <summary> Implments the <c>IBizObjSource</c> interface by maintaining an XML
	/// <c>org.dom4j.Document</c> for its data store.
	/// *
	/// </summary>
	/// <author> mdenman
	/// 
	/// </author>
	[Serializable]
	public class XmlBizSource : IBizObjSource
	{
		
		public XmlBizSource(string info)
		{
			string error = "";
			try
			{
				m_doc = new XmlDocument();
				m_doc.LoadXml(info);
			}
			catch (XmlException e)
			{
				try
				{
					m_doc.Load(info);
				}
				catch(XmlException e2)
				{
					error = e2.ToString();
				}
			}
			
			//TODO: Throw exception based on error string not being empty.
		}

		public virtual IBizObjList getList(string objType, string info)
		{
			if(Application.currentApp.bizObjMgr is BizObjManager)
			{
				BizObjManager bizObjMgr = (BizObjManager)Application.currentApp.bizObjMgr;
				ListState state = null;
				if(info == null)
				{
					info = "*";
					state = new ListState(m_doc.DocumentElement, info, true);
				}
				else
					state = new ListState(m_doc.DocumentElement, info);
				return new XmlBusinessObjectList(bizObjMgr.getBizObjDefinition(objType), state,this);
			}
			else
				throw new Exception("Bad configuration.  Cannot mix different implementations of BizObj together");
		}
		
		public virtual IBizObjCursor getCursor(string objType, string info)
		{
			IBizObjList l = getList(objType, info);
			if (l != null)
				return l.createCursor();
			return null;
		}

		public virtual IBizObj getObject(string objType, string info)
		{
			IBizObj obj = null;
			if (info == null || info.Length == 0)
				obj = new XmlBusinessObject(((BizObjManager)Application.currentApp.bizObjMgr).getBizObjDefinition(objType), m_doc.DocumentElement);
			else
				obj = new XmlBusinessObject(((BizObjManager)Application.currentApp.bizObjMgr).getBizObjDefinition(objType),
#if(!COMPACT_FRAMEWORK)
					(XmlElement) m_doc.DocumentElement.SelectSingleNode(info));
#else
						Cmn.findChildWithName(m_doc.DocumentElement, info));
#endif
			return obj;
		}

		public virtual IBizObj getObject(string objType, string fieldList, object matchValue)
		{
			return getObject(objType,fieldList,matchValue,false);
		}

		public virtual IBizObj getObject(string objType, string fieldList, object matchValue, bool findNearestMatch)
		{
			//TODO: Implement this.
			return null;
		}
		protected XmlDocument m_doc;
		public void Clear()
		{
			m_doc = new XmlDataDocument();
		}
		public void RemoveList(string name)
		{
			//Nothing to do here.
		}
		/// <summary>
		/// Adds a new list reference to the ArrayList of BizObjLists.
		/// </summary>
		/// <param name="thisList"></param>
		public void AddList(IBizObjList thisList)
		{
			//Nothing to do here.
		}
	}
}